package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import model.Cell;
import model.Player;

public class Cell_view_board extends JPanel {
	String kind;
	String name;
	Font font = new Font("Serif", Font.BOLD, 12);
	private Image img=null;
	ArrayList <Player> players=null;
	Player owner;
	int index=0;
	public Cell_view_board (Cell c,int i){
		this.kind=c.getKindCell();
		this.name=c.getName();
		this.players=c.getPlayer();
		this.index=i;
		this.owner=c.get_owner();
		do_cell2();
	}

	public void do_cell2(){
		if(index>0 && index<10){
			this.setPreferredSize(new Dimension(25,34));
		}
		else if(index>10 && index<20) {
			this.setPreferredSize(new Dimension(34,25));
		}
		else if (index>20 && index<30){
			this.setPreferredSize(new Dimension(25,34));
		}
		else if(index>30 && index<40){
			this.setPreferredSize(new Dimension(34,25));
		}
		//if the place is own by bank, line will be black
		if(owner==null || owner.getName().equals("bank")  ){
			this.setBorder(BorderFactory.createLineBorder(Color.black));
		}
		//if the place is owned by a player, line will be the color's player
		else{
			this.setBorder(BorderFactory.createLineBorder(Board_view.player_color.get(owner.getName())));
		}

		if(!kind.equals("city")&& !kind.equals("station")&&!kind.equals("utility")){
			this.setBackground(Color.gray);

		}
		if(kind.equals("go")||kind.equals("go_to_jail")||kind.equals("jail")||kind.equals("free_parking")){
			this.setPreferredSize(new Dimension(35,35));
			this.revalidate();
			this.repaint();

			if(kind.equals("go")){
				img=new ImageIcon("img/go.jpg").getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT);
			}
			else if(kind.equals("go_to_jail")){
				img=new ImageIcon("img/gotojail.jpg").getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT);
			}
			else if(kind.equals("jail")){
				img=new ImageIcon("img/justvisiting.jpg").getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT);
			}
			else{
				img=new ImageIcon("img/freeparking2.jpg").getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT);
			}
		}
	}

	public void paintComponent (Graphics g){
		super.paintComponent(g);
		if(kind.equals("go")||kind.equals("go_to_jail")||kind.equals("jail")||kind.equals("free_parking")){
			g.drawImage(img, 0, 0, this);
		}
		if(!players.isEmpty()){
			for(int i=0;i<players.size();i++){
				//if prisoner
				if(players.get(i).getPrisonerStatut()){
					g.setColor(Board_view.player_color.get(players.get(i).getName()));
					g.fillRect(10,10, 5, 5);
				}
				//if free
				else{
					if(players.size()==1){
						g.setColor(Board_view.player_color.get(players.get(i).getName()));
						g.fillRect(10,10, 5, 5);
					}
					else if(players.size()==2){
						if(i==0){
							g.setColor(Board_view.player_color.get(players.get(0).getName()));
							g.fillRect(10,10, 5, 5);
						}
						else{
							g.setColor(Board_view.player_color.get(players.get(1).getName()));
							g.fillRect(20,10, 5, 5);
						}
					}
					else if(players.size()==3){
						if(i==0){
							g.setColor(Board_view.player_color.get(players.get(0).getName()));
							g.fillRect(10,10, 5, 5);
						}
						else if(i==1){
							g.setColor(Board_view.player_color.get(players.get(1).getName()));
							g.fillRect(20,10, 5, 5);
						}
						else{
							g.setColor(Board_view.player_color.get(players.get(2).getName()));
							g.fillRect(20,20, 5, 5);
						}
					}
					else if(players.size()==4){
						if(i==0){
							g.setColor(Board_view.player_color.get(players.get(0).getName()));
							g.fillRect(10,10, 5, 5);
						}
						else if(i==1){
							g.setColor(Board_view.player_color.get(players.get(1).getName()));
							g.fillRect(20,10, 5, 5);
						}
						else if(i==2){
							g.setColor(Board_view.player_color.get(players.get(2).getName()));
							g.fillRect(20,20, 5, 5);
						}
						else{
							g.setColor(Board_view.player_color.get(players.get(3).getName()));
							g.fillRect(10,20, 5, 5);
						}
					}
					else if(players.size()==5){
						if(i==0){
							g.setColor(Board_view.player_color.get(players.get(0).getName()));
							g.fillRect(10,10, 5, 5);
						}
						else if(i==1){
							g.setColor(Board_view.player_color.get(players.get(1).getName()));
							g.fillRect(20,10, 5, 5);
						}
						else if(i==2){
							g.setColor(Board_view.player_color.get(players.get(2).getName()));
							g.fillRect(20,20, 5, 5);
						}
						else if(i==3){
							g.setColor(Board_view.player_color.get(players.get(3).getName()));
							g.fillRect(10,20, 5, 5);
						}
						else{
							g.setColor(Board_view.player_color.get(players.get(4).getName()));
							g.fillRect(1,20, 5, 5);
						}
					}
					else {
						if(i==0){
							g.setColor(Board_view.player_color.get(players.get(0).getName()));
							g.fillRect(10,10, 5, 5);
						}
						else if(i==1){
							g.setColor(Board_view.player_color.get(players.get(1).getName()));
							g.fillRect(20,10, 5, 5);
						}
						else if(i==2){
							g.setColor(Board_view.player_color.get(players.get(2).getName()));
							g.fillRect(20,20, 5, 5);
						}
						else if(i==3){
							g.setColor(Board_view.player_color.get(players.get(3).getName()));
							g.fillRect(10,20, 5, 5);
						}
						else if(i==4){
							g.setColor(Board_view.player_color.get(players.get(4).getName()));
							g.fillRect(1,20, 5, 5);
						}
						else{
							g.setColor(Board_view.player_color.get(players.get(5).getName()));
							g.fillRect(1,10, 5, 5);
						}
					}
				}
			}
		}
	}
}
